package entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.*;

import org.joda.time.DateTime;
import org.joda.time.Days;

import ejb.dto.HotelAvailabilityDTO;


/**
 * The persistent class for the hotel_availability database table.
 * 
 */
@Entity
@Table(name="hotel_availability", uniqueConstraints=@UniqueConstraint(columnNames={"HOTEL_ID", "ROOM_ID", "FROM_DATE", "TO_DATE"}))
@NamedQueries({
	@NamedQuery(name="HotelAvailability.findAll", query="SELECT h FROM HotelAvailability h"),
	@NamedQuery(name="HotelAvailability.findByHotel", query="SELECT h FROM HotelAvailability h WHERE h.hotel = :hot")
})
public class HotelAvailability implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	@Column(name="FROM_DATE")
	private Date fromDate;

	private float price;

	private int remaining;

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	@Column(name="TO_DATE")
	private Date toDate;

	/**
	 * bi-directional many-to-one association to Hotel
	 */
	@ManyToOne
	private Hotel hotel;

	/**
	 * bi-directional many-to-one association to Room
	 */
	@ManyToOne
	private Room room;
	
	@OneToMany(mappedBy="hotelDates")
	private List<Reservation> reservations;

	//CONSTRUCTORS
	public HotelAvailability() {
	}

	public HotelAvailability(HotelAvailabilityDTO av, Hotel hotel, Room room){
		this.id = av.getId();
		this.fromDate = av.getFromDate();
		this.price = av.getPrice();
		this.remaining = av.getRemaining();
		this.toDate = av.getToDate();
		this.hotel = hotel;
		this.room = room;
	}
	
	//GETTER AND SETTER
	
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getFromDate() {
		return this.fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public float getPrice() {
		return this.price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getRemaining() {
		return this.remaining;
	}

	public void setRemaining(int remaining) {
		this.remaining = remaining;
	}

	public Date getToDate() {
		return this.toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public Hotel getHotel() {
		return this.hotel;
	}

	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}

	public Room getRoom() {
		return this.room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}
	
	public List<Reservation> getReservations() {
		return reservations;
	}

	public void setReservations(List<Reservation> reservations) {
		this.reservations = reservations;
	}
	
	//METHODS
	
	/**
	 * Return days between two Dates
	 * @return days between two Dates
	 */
	public int getDays() {
		return Days.daysBetween(new DateTime(fromDate), new DateTime(toDate)).getDays();
	}


}